#!/usr/bin/env python
# -*- codeing: utf-8 -*-
# @Time : 2021-07-13 21:01
# @Author : Wu Yizhang
# @Site ：www.kychina.com
# @File: view.py
# @Software: PyCharm
# @Function: 
# @python：3.6.8
# @wudada
from django.http import JsonResponse


def my_decorator(func):
    """自定义的装饰器，判断是否登录"""
    def wrapper(request, *args, **kwargs):
        if request.user.is_authenticated:
            # 如果用户登录，则进入这里正常执行
            return func(request, *args, **kwargs)
        else:
            # 如果用户满意登录则进入这里返回400状态码
            return JsonResponse({
                "code":400,
                "errmsg":'请登录后再试'
            })
    return wrapper


class LoginRequiredMixin(object):
    "自定义的Mixin扩展类"
    # 重写的as_view方法
    @classmethod
    def as_view(cls, **initkwargs):
        view = super().as_view(*initkwargs)
        return my_decorator(view)